<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\shop\controller;
use data\extend\YangCai as YangCai;
use data\service\Member as Member;
use data\model\YcUserModel as YcUserModel;
use data\model\YcUnitModel as YcUnitModel;
use data\model\UserModel as UserModel;
use think\Controller;
use think\Session;

/**
 * Description of ReceiveCode
 *
 * @author pcy
 */
class Receivecode extends Controller
{
    public function index()
    {
        $code = request()->get('code');
        
        $accessToken = YangCai::getAccessToken($code);
        
        $userInfoRs = YangCai::getUserInfo($accessToken);
        
        $unitInfoRs = YangCai::getUnitInfo($accessToken);
        
        if ( !$userInfoRs['success'] )
        {
            $this->error('获取用户信息失败', __URL(__URL__ . "/index"));
        }
        
        if ( !$unitInfoRs['success'] )
        {
            $this->error('获取用户单位信息失败', __URL(__URL__ . "/index"));
        }
        
        $userAttributes = $userInfoRs['rows'];
        
        $unitAttributes = $unitInfoRs['rows'];
        
        $usercode = $userAttributes['usercode'];
        
        $unitcode = $unitAttributes['unitcode'];
        
        $ycUserModel = new YcUserModel();
        
        $ycUser = $ycUserModel->getInfo("usercode='$usercode'");
        
        if ( empty( $ycUser ) )//如果本地不存则添加
        {
            $userAttributes['unitcode'] = $unitcode;
            $ycUserModel->save($userAttributes);
        }
        else if ( $ycUser['updatetime'] < $userAttributes['updatetime'] )//如果本地数据不是最新，则更新
        {
            $ycUserModel->save($userAttributes, ['usercode'=>$usercode]);
        }
        
        $ycUnitModel = new YcUnitModel();
        
        $ycUnit = $ycUnitModel->getInfo("unitcode='$unitcode'");
        
        if ( empty( $ycUnit ) )//如果本地不存则添加
        {
            $ycUnitModel->save($unitAttributes);
        }
        else if ( $ycUnit['updatedat'] < $unitAttributes['updatedat'] )//如果本地数据不是最新，则更新
        {
            $ycUnitModel->save($unitAttributes, ['unitcode'=>$unitcode]);
        }
 
        $mobile = $userAttributes['phone'];
        
        $member = new Member();
        
        $exist = $member->memberIsMobile($mobile);
        
        if ( $exist )//如果已经存在，直接登录
        {
            $rs = $member->login($mobile);
        }
        else
        {
            $user_qq_id = $userAttributes['703291111'];

            $user_name = '';

            $password = self::_getRandPwd();

            $rs = $member->registerMember($user_name, $password, '', $mobile, $user_qq_id, '', '', '', '');

            if ( $rs <= 0 )
            {
                $this->error('央采用户自动注册失败');
            }
        }

        if ( $rs > 0 )
        {
            $login_pre_url = empty( $_SESSION['login_pre_url'] ) ? 'index/index' : $_SESSION['login_pre_url'];
        
            $this->success('登录成功', $login_pre_url);
        }
        else
        {
            $this->error('登录失败', 'index/index');
        }

    }
    
    function _getRandPwd($pw_length = 8)
    {
        $randpwd = '';
        
        for ($i = 0; $i < $pw_length; $i++) 
        {
            $randpwd .= chr(mt_rand(33, 126));
        }
        
        return $randpwd;
    }
}
